﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Newtonsoft.Json.Linq;

namespace DataIssue.Service
{
    public class StockCommentService
    {
        /// <summary>
        /// 千股千评
        /// </summary>
        /// <returns></returns>
        public List<Entity.StockComment> GetStocks()
        {
            List<Entity.StockComment> datas = new List<Entity.StockComment>();
            F10ParamEntity param = new F10ParamEntity("Base", "V_BS_COMMENT", 2);
            //param.Filters = new List<string>();
            //param.Filters.Add("STCPD-int=1");
            param.Limit = 8;
            param.OrderBy = "ZDF DESC";

            JToken clientData;
            int totalCount = 0;
            F10Query.TryParse(param, false, out clientData, out totalCount);
            if (clientData != null && totalCount > 0)
            {
                for (int i = 0; i < clientData.Count(); i++)
                {
                    Entity.StockComment data = new Entity.StockComment();
                    data.Code = clientData[i].J_String("STOCKCODE");
                    data.Comment = clientData[i].J_String("COMMENT");
                    data.Name = clientData[i].J_String("STOCKSNAME");
                    datas.Add(data);
                }
            }
            return datas;
        }

        /// <summary>
        /// 投资评级
        /// </summary>
        /// <returns></returns>
        public List<Entity.StockComment> GetTouZiPingJi()
        {
            List<Entity.StockComment> datas = new List<Entity.StockComment>();
            F10ParamEntity param = new F10ParamEntity("Base", "V_BS_INVESTRANK", 2);
            //param.Filters = new List<string>();
            //param.Filters.Add("STCPD-int=1");
            param.Limit = 8;
            param.OrderBy = "WRITEDATE DESC";

            JToken clientData;
            int totalCount = 0;
            F10Query.TryParse(param, false, out clientData, out totalCount);
            if (clientData != null && totalCount > 0)
            {
                for (int i = 0; i < clientData.Count(); i++)
                {
                    Entity.StockComment data = new Entity.StockComment();
                    data.Code = clientData[i].J_String("SEC_CODE");
                    data.Comment = clientData[i].J_String("INVEST_RATING");
                    data.Name = clientData[i].J_String("SEC_SNAME");
                    data.YanJiuJiGou = clientData[i].J_String("ORGNAME");
                    data.YanJiuJiGouDaiMa = clientData[i].J_String("ORGCODE");
                    datas.Add(data);
                }
            }
            return datas;
        }

        /// <summary>
        /// 盈利预测
        /// </summary>
        /// <returns></returns>
        public List<Entity.StockComment> GetYingLiYuCe()
        {
            List<Entity.StockComment> datas = new List<Entity.StockComment>();
            F10ParamEntity param = new F10ParamEntity("Base", "V_JRJ_COM_PFT_FCST", 2);
            //param.Filters = new List<string>();
            //param.Filters.Add("DATE-str=今年以来");
            param.Limit = 8;
            param.OrderBy = "YEAR2_AVG DESC";

            JToken clientData;
            int totalCount = 0;
            F10Query.TryParse(param, false, out clientData, out totalCount);
            if (clientData != null && totalCount > 0)
            {
                for (int i = 0; i < clientData.Count(); i++)
                {
                    double temp = 0f;
                    Entity.StockComment data = new Entity.StockComment();
                    data.Code = clientData[i].J_String("SEC_CODE");
                    data.Name = clientData[i].J_String("SEC_SNAME");
                    data.ProfitOn14 = clientData[i].J_Double("YEAR3_AVG", temp);
                    data.ProfitOn12 = clientData[i].J_Double("YEAR1_AVG", temp);
                    data.ProfitOn13 = clientData[i].J_Double("YEAR2_AVG", temp);
                    datas.Add(data);
                }
            }
            return datas;
        }
    }
}